package com.cys.dm.rest.service;

import com.cys.dm.client.base.DmImageClient;
import com.cys.dm.client.item.DmCinemaClient;
import com.cys.dm.client.item.DmItemClient;
import com.cys.dm.client.item.DmItemTypeClient;
import com.cys.dm.client.scheduler.DmSchedulerClient;
import com.cys.dm.client.scheduler.DmSchedulerSeatPriceClient;
import com.cys.dm.entity.base.DmImage;
import com.cys.dm.entity.item.DmCinema;
import com.cys.dm.entity.item.DmItem;
import com.cys.dm.entity.item.DmItemType;
import com.cys.dm.entity.scheduler.DmScheduler;
import com.cys.dm.entity.scheduler.DmSchedulerSeatPrice;
import com.cys.dm.vo.CommonResponse;
import com.cys.dm.vo.VoUtil;
import com.cys.dm.vo.dmitem.DmItem2Response;
import com.cys.dm.vo.dmitem.DmItemIndexSecondLevelVo;
import com.cys.dm.vo.dmitem.DmItemPriceVo;
import com.cys.dm.vo.dmitem.SeniorityResponse;
import com.cys.dm.vo.itemlist.DmMultipleItemVo;
import com.cys.dm.vo.itemlist.DmRecommendVo;
import com.cys.dm.vo.itemlist.QueryItemList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DmItemService {
    @Autowired
    private DmItemClient dmItemClient;
    @Autowired
    private DmImageClient dmImageClient;
    @Autowired
    private DmSchedulerClient dmSchedulerClient;
    @Autowired
    private DmSchedulerSeatPriceClient dmSchedulerSeatPriceClient;
    @Autowired
    private DmCinemaClient dmCinemaClient;

    /**
     * 今日推荐
     * @return
     */
    public CommonResponse<List<DmItem2Response>> todayRecommend(){
        List<DmItem2Response> dmItem2Responses = new ArrayList<>();
        //根据商品ID查询对应的图片
        List<DmItem> dmItems =  dmItemClient.todayRecommend();
        dmItems.forEach(dmitem->{
            DmImage dmImage = dmImageClient.findItenPicByID(dmitem.getId());
            DmItem2Response dmItem2Response = new DmItem2Response();
            dmItem2Response.setId(dmitem.getId());
            dmItem2Response.setItemName(dmitem.getItemName());
            dmItem2Response.setMinPrice(dmitem.getMinPrice());
            if (dmImage!=null){
                dmItem2Response.setImgUrl(dmImage.getImgUrl());
            }
            dmItem2Responses.add(dmItem2Response);
        });

        return VoUtil.returnSuccess("今日推荐查询成功",dmItem2Responses);
    };

    /**
     * 即将开售
     * @return
     */
    public CommonResponse<List<DmItem2Response>> queryToSaleItem(){
        List<DmItem2Response> dmItem2Responses = new ArrayList<>();
        //根据商品ID查询对应的图片
        List<DmItem> dmItems =  dmItemClient.queryToSaleItem();
        dmItems.forEach(dmitem->{
            DmImage dmImage = dmImageClient.findItenPicByID(dmitem.getId());
            DmItem2Response dmItem2Response = new DmItem2Response();
            dmItem2Response.setId(dmitem.getId());
            dmItem2Response.setItemName(dmitem.getItemName());
            dmItem2Response.setMinPrice(dmitem.getMinPrice());
            if (dmImage!=null){
                dmItem2Response.setImgUrl(dmImage.getImgUrl());
            }
            dmItem2Responses.add(dmItem2Response);
        });

        return VoUtil.returnSuccess("即将开售查询成功",dmItem2Responses);
    }

    /**
     * 根据类型ID查询推荐
     * @param itemTypeId
     * @return
     */
    public CommonResponse<List<SeniorityResponse>> postIndexSeniority(Long itemTypeId){
        List<SeniorityResponse> seniorityResponses = new ArrayList<>();
        List<DmItem> dmItems = dmItemClient.postIndexSeniority(itemTypeId);
        dmItems.forEach(dmItem -> {
            SeniorityResponse seniorityResponse = new SeniorityResponse();
            seniorityResponse.setId(dmItem.getId());
            seniorityResponse.setItemName(dmItem.getItemName());
            seniorityResponse.setItemTypeId(itemTypeId);
            seniorityResponses.add(seniorityResponse);
        });
        return VoUtil.returnSuccess("排行查询成功",seniorityResponses);
    }

    /**
     * 根据商品ID查询排期
     *
     * @param itemId
     * @return
     */
    public List<DmScheduler> getTimePlan(Long itemId) {
        return dmSchedulerClient.findSchedulerByItemId(itemId);
    }

    /**
     * 根据排期ID查询排期价格
     *
     * @param scheduleId
     * @return
     */
    public List<DmItemPriceVo> getPrice(Long scheduleId) {
        //源数据
        List<DmSchedulerSeatPrice> dmSchedulerSeatPriceList = dmSchedulerSeatPriceClient.findSchedulerSeatPriceBySchedulerId(scheduleId);
        //vo类
        List<DmItemPriceVo> voList = new ArrayList<>();
        dmSchedulerSeatPriceList.forEach(dmSchedulerSeatPrice -> {
            DmItemPriceVo vo = new DmItemPriceVo();
            BeanUtils.copyProperties(dmSchedulerSeatPrice, vo);
            vo.setPrice(dmSchedulerSeatPrice.getPrice());
            if (dmSchedulerSeatPrice.getSeatNum() > 0) {
                vo.setIsHaveSeat(1);
            } else {
                vo.setIsHaveSeat(0);
            }

            voList.add(vo);
        });
        return voList;
    }

    /**
     * 根据分类查询猜你喜欢轮播图
     *
     * @param map
     * @return
     */
    public List<DmItemIndexSecondLevelVo> getCarouselData(Map<String, Integer> map) {
        //解析数据
        Integer itemTypeId = map.get("itemTypeId");


        //源数据
        List<DmItem> dmItemsList = dmItemClient.findHotItem(Long.parseLong(itemTypeId.toString()), 1L, 1L, 0L, 5L);


        //Vo数据
        List<DmItemIndexSecondLevelVo> voList = new ArrayList<>();

        dmItemsList.forEach(dmItem -> {
            DmItemIndexSecondLevelVo vo = new DmItemIndexSecondLevelVo();
            vo.setId(dmItem.getId());
            vo.setItemName(dmItem.getItemName());
            vo.setMinPrice(dmItem.getMinPrice());
            //查询图片
            DmImage image = dmImageClient.findItenPicByID(vo.getId());
            if (image != null) {
                vo.setImgUrl(image.getImgUrl());
            }

            voList.add(vo);
        });
        return voList;
    }

    /**
     * 亲子首页 猜你喜欢商品列表
     *
     * @param map
     * @return
     */
    public List<QueryItemList> getGuessYouLike(Map<String, Long> map) {
        //解析数据
        Long itemTypeId = map.get("itemTypeId");

        //源数据
        List<DmItem> hotItem = dmItemClient.findHotItem(itemTypeId, 1L, 1L, 0L, 6L);

        //vo数据
        List<QueryItemList> voList = new ArrayList<>();

        hotItem.forEach(dmItem -> {
            //将商品转换为楼层Vo的Item项
            QueryItemList itemVo = new QueryItemList();

            //分类主键
            itemVo.setId(dmItem.getId());
            //节目名称
            itemVo.setItemName(dmItem.getItemName());

            //城市
            DmCinema dmCinema = dmCinemaClient.findById(dmItem.getCinemaId());
            itemVo.setAreaId(dmCinema.getAreaId());

            //城市名称
            itemVo.setAreaName(dmCinema.getAreaName());

            //城市所在地址
            itemVo.setAddress(dmCinema.getAddress());

            //节目开始时间
            itemVo.setStartData(dmItem.getStartTime());

            //节目结束时间
            itemVo.setEndDate(dmItem.getEndTime());

            //节目宣传片
            DmImage image = dmImageClient.findItenPicByID(dmItem.getId());
            if (image != null) {
                itemVo.setImgUrl(image.getImgUrl());
            }

            //最低价格
            itemVo.setMinPrice(dmItem.getMinPrice());

            voList.add(itemVo);
        });

        return voList;
    }

    /**
     * 根据年龄分组查询商品
     *
     * @param map
     * @return
     */
    public List<QueryItemList> getParentChild(Map<String, Integer> map) {
        //解析数据
        Integer ageGroup = map.get("ageGroup");
        Integer limit = map.get("limit");

        //源数据
        List<DmItem> dmItemList = dmItemClient.findDmItemByAgeAndPageSize(ageGroup, limit);

        //vo数据
        List<QueryItemList> voList = new ArrayList<>();

        dmItemList.forEach(dmItem -> {
            //将商品转换为楼层Vo的Item项
            QueryItemList itemVo = new QueryItemList();

            //分类主键
            itemVo.setId(dmItem.getId());
            //节目名称
            itemVo.setItemName(dmItem.getItemName());

            //城市
            DmCinema dmCinema = dmCinemaClient.findById(dmItem.getCinemaId());
            itemVo.setAreaId(dmCinema.getAreaId());

            //城市名称
            itemVo.setAreaName(dmCinema.getAreaName());

            //城市所在地址
            itemVo.setAddress(dmCinema.getAddress());

            //节目开始时间
            itemVo.setStartData(dmItem.getStartTime());

            //节目结束时间
            itemVo.setEndDate(dmItem.getEndTime());

            //节目宣传片
            DmImage image = dmImageClient.findItenPicByID(dmItem.getId());
            if (image != null) {
                itemVo.setImgUrl(image.getImgUrl());
            }

            //最低价格
            itemVo.setMinPrice(dmItem.getMinPrice());

            voList.add(itemVo);
        });

        return voList;
    }

    /**
     * 首页时间查询
     * @param param
     * @return
     */
    public CommonResponse<List<DmMultipleItemVo>> getCalendarData(Map<String,String> param){
        Long itemTypeId = Long.valueOf(param.get("itemTypeId"));
        List<DmItem> parentChild = dmItemClient.getCalendarData(itemTypeId);
        List<DmMultipleItemVo> dmMultipleItemVos = new ArrayList<>();
        //分组查询
        parentChild.forEach(parent ->{
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            List<DmItem> calendarData2 = dmItemClient.getCalendarData2(parent.getId(),df.format(parent.getStartTime()));
            calendarData2.forEach(data ->{
                DmMultipleItemVo dmMultipleItemVo = new DmMultipleItemVo();
                DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyyMMdd");
                dmMultipleItemVo.setDay(df2.format(parent.getStartTime()));
                List<DmRecommendVo> dmRecommendVos = new ArrayList<>();
                DmRecommendVo dmRecommendVo2 = new DmRecommendVo();
                BeanUtils.copyProperties(data,dmRecommendVo2);
                //图片
                dmRecommendVo2.setImgUrl(dmImageClient.findItenPicByID(data.getId()).getImgUrl());
                //区域
                DmCinema byId = dmCinemaClient.findById(data.getCinemaId());
                dmRecommendVo2.setAreaId(byId.getAreaId());
                dmRecommendVo2.setAreaName(byId.getAreaName());
                dmRecommendVo2.setAddress(byId.getAddress());
                //时间
                dmRecommendVo2.setStareDate(data.getStartTime());
                dmRecommendVo2.setEndDate(data.getEndTime());
                dmRecommendVos.add(dmRecommendVo2);
                dmMultipleItemVo.setItemList(dmRecommendVos);
                dmMultipleItemVos.add(dmMultipleItemVo);
            });
        });
        return VoUtil.returnSuccess("查询成功",dmMultipleItemVos);
    }


}
